package chances.epg.data.taglib;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.commons.utils.JSONUtils;
import chances.epg.navigator.context.AccessContextUtils;
import chances.epg.search.searcher.SearchRequest;
import chances.epg.search.searcher.SearchResult;
import chances.epg.search.searcher.SearchService;



public class SearchTag extends QueryListTag {
	private static Logger logger = EpgLogFactory.getSystemLogger();

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -7707793322127490199L;

	private String action;
	private String condition;
	private int size = 10;
	private String type;
	private String contentCode;
	private String hdType;

	@Override
	public int doEndTag() throws JspException {
		SearchResult result = new SearchResult();

		SearchRequest res = null;
		try {
			res = this.createSearchRequest();
			String action = res.getAction();
			if ("byPerson".equals(action)) {
				result = this.getSearchService().getSearchHelper().searchByPersons(res);
			} else if ("byTag".equals(action)) {
				result = this.getSearchService().getSearchHelper().searchByTags(res);
			} else {
				logger.error("Unsupport search mothed,action=" + action);
			}

			List<Map<String, Object>> contents = new ArrayList<Map<String, Object>>();
			for (Object obj : result.getResult()) {
				Map<String, Object> c = JSONUtils.toMap(obj.toString());
				c.put("url", navigateUrlFactory.generateSearchFilterURL(
						AccessContextUtils.getAccessContext(this.pageContext), c));
				contents.add(c);
			}
			this.setCollectionResult(contents);
			
			/*SearchContent searchContent = null;
			List<SearchContent> ret = new ArrayList<SearchContent>();
			for (Object object : result.getResult()) {
				String jobj = JSONUtils.fromObject(object);
				searchContent = (SearchContent) JSONUtils.toObject(jobj,
						SearchContent.class);
				searchContent.setUrl(navigateUrlFactory.generateURL(
						AccessContextUtils.getAccessContext(this.pageContext),
						searchContent));
				ret.add(searchContent);
			}
			
			this.setCollectionResult(ret);*/
		} catch (Throwable ex) {
			logger.error("SearchTag error", ex);
		}

		return super.doEndTag();
	}

	private SearchService getSearchService() {
		return (SearchService) this.getBean("searchService");
	}

	/**
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private SearchRequest createSearchRequest()
			throws UnsupportedEncodingException {
		SearchRequest res = new SearchRequest();
		res.setAction(this.action);

		if (StringUtils.isNotBlank(this.condition)) {
			// res.setCondition(URLDecoder.decode(this.condition, "utf-8"));
			res.setCondition(this.condition);
		}

		res.setPagesize(this.size);

		if (StringUtils.isNotEmpty(this.type)) {
			res.setType(this.type);
		}
		
		if(StringUtils.isNotBlank(this.contentCode)){
			res.setContentCode(this.contentCode);
		}
		
		if(StringUtils.isNotBlank(this.hdType)){
			res.setHdType(this.hdType);
		}
		
		return res;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getContentCode() {
		return contentCode;
	}

	public void setContentCode(String contentCode) {
		this.contentCode = contentCode;
	}

	public String getHdType() {
		return hdType;
	}

	public void setHdType(String hdType) {
		this.hdType = hdType;
	}
}
